import 'package:flutter/material.dart';
import 'package:flutter_lui/flutter_lui.dart';
import 'package:flutter_lui/src/l_style.dart';

enum LDividerTextPosition { start, center, end }

class LDivider extends StatelessWidget {
  /// 文字位置
  final LDividerTextPosition textPosition;

  /// 线条方向
  final Axis direction;

  /// 颜色 线条和文字同一个颜色
  final Color? color;

  /// 线条宽度
  final double? stockWidth;

  /// 线条在交叉轴上的间距
  final double? space;

  /// 显示文字
  final String? text;

  /// 文字字号
  final double? fontSize;

  /// 文字在主轴上的间距
  final double? textSpace;

  /// 外边距
  /// - [1]=>all
  /// - [1,1]=>[上下，右左]
  /// - [1,1,1]=> [上，右左，下]
  /// - [1,1,1,1]=> [上，右，下，左]
  final List<double> margin;

  /// 左外边距
  final double? marginLeft;

  /// 右外边距
  final double? marginRight;

  /// 上外边距
  final double? marginTop;

  /// 下外边距
  final double? marginBottom;

  /// 上下外边距
  final double? marginVertical;

  /// 左右外边距
  final double? marginHorizontal;

  const LDivider(
      {this.textPosition = LDividerTextPosition.center,
      this.direction = Axis.horizontal,
      this.space,
      this.color,
      this.stockWidth,
      this.text,
      this.fontSize,
      this.textSpace,
      this.margin = const [0, 0, 0, 0],
      this.marginLeft,
      this.marginRight,
      this.marginTop,
      this.marginBottom,
      this.marginVertical,
      this.marginHorizontal,
      Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    var useStockWidth = stockWidth ?? lrpx(1);
    var useSpace = space ?? lrpx(10);
    var useTextSpace = textSpace ?? lrpx(5);
    List<Widget> childs = [];
    Widget useLineView = LBox(
      flex: 1,
      width: direction == Axis.horizontal ? null : useStockWidth,
      height: direction == Axis.vertical ? null : useStockWidth,
      backgroundColor: color ?? LUI.instance.borderColor,
    );

    childs.add(useLineView);

    if (text?.isNotEmpty == true) {
      var textView = LBox(
        padding: direction == Axis.horizontal
            ? [0, useTextSpace]
            : [useTextSpace, 0],
        child: text!,
        fontSize: fontSize ?? LStyle.fontSize,
        color: color ?? LUI.instance.borderColor,
        textAlign: TextAlign.center,
      );
      if (textPosition == LDividerTextPosition.start) {
        childs.insert(0, textView);
      } else if (textPosition == LDividerTextPosition.center) {
        childs.add(textView);
        childs.add(useLineView);
      } else if (textPosition == LDividerTextPosition.end) {
        childs.add(textView);
      }
    }

    return LBox(
      margin: margin,
      marginLeft: marginLeft,
      marginRight: marginRight,
      marginTop: marginTop,
      marginBottom: marginBottom,
      marginVertical: marginVertical,
      marginHorizontal: marginHorizontal,
      alignment: Alignment.center,
      width: direction == Axis.horizontal ? null : useSpace * 2,
      height: direction == Axis.vertical ? null : useSpace * 2,
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      direction: direction == Axis.horizontal
          ? BoxDirections.horizontal
          : BoxDirections.vertical,
      children: childs,
    );
  }
}
